//
//  DCSendOrderViewController.m
//  Driver
//
//  Created by 大橙子 on 2018/11/8.
//  Copyright © 2018 Tomous. All rights reserved.
//

#import "DCSendOrderViewController.h"
#import "DCSendOrderCell.h"
#import "DCOrderDetailViewController.h"
#import "DCMapViewController.h"

#define MAXSTRINGLENGTH 50    //限定输入长度
@interface DCSendOrderViewController ()<UITableViewDelegate,UITableViewDataSource,sendOrderCellDelegate,UITextViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;

@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong) UILabel *textLabel;
@end

@implementation DCSendOrderViewController
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, ViewHeight-50) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellStyleDefault;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackgroundColor;
    }
    return _tableView;
}
-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSArray *array = @[
                       @{
                           @"carrier":@"华阳",
                           @"time":@"2018-8-28 10:15:14",
                           @"type":@"待成交",
                           },
                       @{
                           @"carrier":@"长久",
                           @"time":@"2018-8-29 10:15:14",
                           @"type":@"已成交",
                           },
                       @{
                           @"carrier":@"华阳",
                           @"time":@"2018-8-30 10:15:14",
                           @"type":@"待成交",
                           },
                       @{
                           @"carrier":@"长久",
                           @"time":@"2018-8-31 10:15:14",
                           @"type":@"已成交",
                           },
                       ];
    [self.dataSource removeAllObjects];
    self.dataSource = [DCSendOrderModel mj_objectArrayWithKeyValuesArray:array];
    [self.view addSubview:self.tableView];
}

#pragma tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DCSendOrderCell *cell = [DCSendOrderCell cellWithTableView:tableView];
    cell.model = self.dataSource[indexPath.row];
    cell.delegate = self;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kBaseHeight*5-20;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DCOrderDetailViewController *detailVC = [[DCOrderDetailViewController alloc]initWithOrderID:@"" state:@"state"];
    [[UIViewController currentViewController].navigationController pushViewController:detailVC animated:YES];
}
#pragma sendOrderCellDelegate
-(void)selectedWithCompleteBtn:(NSString *)content
{
//    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认取消订单" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//        DCLog(@"确认取消订单");
//    }];
//    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//    [alertVC addAction:sureAction];
//    [alertVC addAction:cancleAction];
//    [self presentViewController:alertVC animated:YES completion:nil];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self.bgView];
    //动画
    self.bgView.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:.5 animations:^{
        self.bgView.transform = CGAffineTransformIdentity;
    }];
}
-(void)selectedWithLocationBtn:(NSString *)content
{
    DCMapViewController *mapVC = [[DCMapViewController alloc]init];
    [[UIViewController currentViewController].navigationController pushViewController:mapVC animated:YES];
}

-(UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];;
        _bgView.userInteractionEnabled = YES;
        
        //        UITapGestureRecognizer *tapBgView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgViewDidClick)];
        //        [_bgView addGestureRecognizer:tapBgView];
        UIView *contentView = [[UIView alloc]init];
        contentView.size = CGSizeMake(ScreenWidth*0.8, 40*4+100*HEIGHTSCALE6);
        contentView.center = _bgView.center;
        contentView.backgroundColor = [UIColor whiteColor];
        contentView.layer.cornerRadius = 10;
        [_bgView addSubview:contentView];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, contentView.width, 40)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = @"提示";
        [contentView addSubview:titleLabel];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, titleLabel.bottom, contentView.width, 1)];
        line.backgroundColor = BackgroundColor;
        [contentView addSubview:line];
        
        UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, line.bottom, contentView.width, 40)];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.text = @"请填写取消原因";
        [contentView addSubview:textLabel];
        
        _textView = [[UITextView alloc]init];
        _textView.size = CGSizeMake(contentView.width/2+50, 100*HEIGHTSCALE6);
        _textView.x = (contentView.width-_textView.width)/2;
        _textView.y = textLabel.bottom+5;
        _textView.backgroundColor = BackgroundColor;
        _textView.font = FONT(15);
        _textView.delegate = self;
        [contentView addSubview:_textView];
        
        _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 7, _textView.width-5, 40)];
        _textLabel.textColor = [UIColor grayColor];
        _textLabel.font = [UIFont systemFontOfSize:13];
        _textLabel.text = @"请填写取消原因，最多不超过50字";
        _textLabel.numberOfLines = 0;
        [_textView addSubview:_textLabel];
        
        
        CGFloat btnWidth = (contentView.width - 100*WIDTHSCALE6)/2;
        UIButton * surtBtn = [[UIButton alloc]init];
        [surtBtn setTitle:@"确认" forState:UIControlStateNormal];
        [surtBtn setTitleColor:BlueColor forState:UIControlStateNormal];
        surtBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [surtBtn addTarget:self action:@selector(surtBtnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
        surtBtn.layer.cornerRadius = 18;
        surtBtn.layer.masksToBounds = YES;
        surtBtn.layer.borderWidth = 1;
        surtBtn.layer.borderColor = BlueColor.CGColor;
        [contentView addSubview:surtBtn];
        [surtBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(btnWidth);
            make.height.mas_offset(35);
            make.left.equalTo(contentView.mas_left).offset(40);
            make.bottom.equalTo(contentView.mas_bottom).offset(-10);
        }];
        
        UIButton * cancleBtn = [[UIButton alloc]init];
        [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        cancleBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [cancleBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [cancleBtn addTarget:self action:@selector(cancleBtnOnClicked:) forControlEvents:UIControlEventTouchUpInside];
        cancleBtn.layer.cornerRadius = 18;
        cancleBtn.layer.masksToBounds = YES;
        cancleBtn.layer.borderWidth = 1;
        cancleBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
        [contentView addSubview:cancleBtn];
        [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(btnWidth);
            make.height.mas_offset(35);
            make.right.equalTo(contentView.mas_right).offset(-40);
            make.bottom.equalTo(contentView.mas_bottom).offset(-10);
        }];
    }
    return _bgView;
}
-(void)surtBtnOnClicked:(UIButton *)sender {
    
}
-(void)searchBtnDidClick {
    
}
-(void)cancleBtnOnClicked:(UIButton *)sender {
    UIView *view = (UIView *)self.textView.superview.superview;
    view.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:.5 animations:^{
        view.transform = CGAffineTransformMakeScale(0.0000000001, 0.00000001);
    }completion:^(BOOL finished) {
        [view removeFromSuperview];
    }];
}

#pragma UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length>0) {
        _textLabel.hidden = YES;
    }else{
        _textLabel.hidden = NO;
    }
    
    NSString *lang = [[[UITextInputMode activeInputModes] firstObject] primaryLanguage];//当前的输入模式
    if ([lang isEqualToString:@"zh-Hans"])
    {
        UITextRange *range = [textView markedTextRange];
        UITextPosition *start = range.start;
        UITextPosition*end = range.end;
        NSInteger selectLength = [textView offsetFromPosition:start toPosition:end];
        NSInteger contentLength = textView.text.length - selectLength;
        
        if (contentLength > MAXSTRINGLENGTH)
        {
            textView.text = [textView.text substringToIndex:MAXSTRINGLENGTH];
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"最长限制100个字" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil] show];
        }
    }
    else
    {
        if (textView.text.length > MAXSTRINGLENGTH)
        {
            textView.text = [textView.text substringToIndex:MAXSTRINGLENGTH];
            [[[UIAlertView alloc] initWithTitle:@"提示" message:@"最长限制70个字" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil] show];
            
        }
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
